// pages/consult/consult.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList:['财务咨询','法务咨询'],
    activeIndex:0,
    Finance:[],
    Legal:[],
    pageSize:1,
    pageNum:6,
    isPull:true,
    status:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  var pageSize = this.data.pageSize
  var status = this.data.status
  var userinfo = wx.getStorageSync('userInfo')
      this.setData({
        userInfo:userinfo
      })
    this.getFinance(pageSize,status)
  },
 
   onLoadFun:function(s){
     console.log(s)
   },
  getFinance:function(page,type){
    var that = this
    var u = that.data.userInfo
    var data = {
      page:page,
      limit:this.data.pageNum,
      type:1,
      user:u.uid
     }
    request.get('consultation',data,{ noAuth : true}).then(res=>{ 
      console.log(type)
      if(type=='againShow' || type=='switch'){
        that.setData({
          Finance:[]
        })
      }
      if(res.data.length>0){
        that.setData({
          Finance:that.data.Finance.concat(res.data)
        })
     
      }else{
        that.setData({
          isPull:false
        })
      }
    })
  },
  getLegal:function(page,type){
    var that = this
    var u = that.data.userInfo
    var data = {
      page:page,
      limit:this.data.pageNum,
      type:2,
      user:u.uid
     }
    request.get('consultation',data,{ noAuth : true}).then(res=>{ 
      if(type=='againShow' || type=='switch'){
        that.setData({
          Legal:[]
        })
      }
      if(res.data.length>0){
        that.setData({
          Legal:that.data.Legal.concat(res.data)
        })
      }else{
        that.setData({
          isPull:false
        })
      }
    })
  },

  getIndex:function(e){
    console.log('切换')
    var index = e.detail.index
    this.setData({
      pageSize:1,
      activeIndex:index,
       isPull:true,
       status:'switch'
    })
   var page = this.data.pageSize
   var type = this.data.activeIndex
    var status = this.data.status
    if(type==0){
      this.getFinance(page,status)
    }else{
      this.getLegal(page,status)
    }
   
  },

  goMess(){
    var that = this
    var id = ++that.data.activeIndex
    var u = that.data.userInfo
    wx.navigateTo({
      url: '../consult/message/message?type='+id+'&name='+u.nickname
    })
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      pageSize:1,
      status:'againShow',
      isPull:true
    })
    var page = this.data.pageSize
    var type = this.data.activeIndex
    var status = this.data.status
    if(type==0){
      this.getFinance(page,status)
    }else{
      this.getLegal(page,status)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var v = this.data
    this.setData({
      status:''
    })
    if(v.isPull){
      var nextPage = ++v.pageSize
      var type = this.data.activeIndex
      var status = this.data.status
      if(type==0){
        this.getFinance(nextPage,status)
        console.log('下拉')
      }else{
        this.getLegal(nextPage,status)
      }
    }
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})